package com.yellowpage.ndc.action;
  
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.ndc.data.Customer;
import com.yellowpage.ndc.service.CustomerService;

public class CustomerAction extends BaseAction {

    public String RESULT_QUERY = "query";
    public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	
	private Configuration configuration;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
	
	private List<Customer> customerList = new ArrayList<Customer>();
	private List<Map> resultList = new ArrayList<Map>();
	
	private Customer customer;
	private CustomerService customerService;
	
	private String selectedId;	
	private String[] selectedIds;	
	private String message;
	
		private String query_custId;
		private String query_groupId;
		private String query_name1;
		private String query_name2;
		private String query_addrId;
		private String query_addr;
		private String query_tel;
		private String query_mainListingId;
		private String query_mainListingStatus;
		private String query_grade;
		private String query_nonpublic;
		private String query_custStatus;
		private String query_note;
		private String query_modifyTime;
		private String query_modifyEmpId;
		private String query_custTypeCode;
		private String query_classMainId;
		private String query_orgId;
		private String query_url;
		private String query_weibo;
		private String query_weixin;
		private String query_yixin;
		private String query_logo;
		private String query_safeStatus;
		private String query_bxName;
		private String query_pyName;
		private String query_engName;
		
	public void prepare() {
		super.prepare();
	
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.customer = customerService.getCustomerByCustId(selectedId);
		}
	}
	
	public String queryByPage() {		
		initPageRender();
		
		Map queryMap = new HashMap();	
			    queryMap.put("custId", query_custId);
	    	    queryMap.put("groupId", query_groupId);
	    	    queryMap.put("name1", query_name1);
	    	    queryMap.put("name2", query_name2);
	    	    queryMap.put("addrId", query_addrId);
	    	    queryMap.put("addr", query_addr);
	    	    queryMap.put("tel", query_tel);
	    	    queryMap.put("mainListingId", query_mainListingId);
	    	    queryMap.put("mainListingStatus", query_mainListingStatus);
	    	    queryMap.put("grade", query_grade);
	    	    queryMap.put("nonpublic", query_nonpublic);
	    	    queryMap.put("custStatus", query_custStatus);
	    	    queryMap.put("note", query_note);
	    	    queryMap.put("modifyTime", query_modifyTime);
	    	    queryMap.put("modifyEmpId", query_modifyEmpId);
	    	    queryMap.put("custTypeCode", query_custTypeCode);
	    	    queryMap.put("classMainId", query_classMainId);
	    	    queryMap.put("orgId", query_orgId);
	    	    queryMap.put("url", query_url);
	    	    queryMap.put("weibo", query_weibo);
	    	    queryMap.put("weixin", query_weixin);
	    	    queryMap.put("yixin", query_yixin);
	    	    queryMap.put("logo", query_logo);
	    	    queryMap.put("safeStatus", query_safeStatus);
	    	    queryMap.put("bxName", query_bxName);
	    	    queryMap.put("pyName", query_pyName);
	    	    queryMap.put("engName", query_engName);
	    	    	
		resultList = customerService.queryCustomerByPage(queryMap, pageRender);
		
		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	
	public String toAdd() {
		return RESULT_ADD;
	}
	
	public String save() {
	    
		customerService.saveCustomer(customer);		
		
		message = "新增成功!";
		return RESULT_ADD;
	}
	
	public void validateSave() {
		if (customer.getCustId().length() == 0) {
			this.addActionError("** 客户编号不能为空 !");
		}
		if (customer.getGroupId().length() == 0) {
			this.addActionError("** 品牌编号不能为空 !");
		}
		if (customer.getName1().length() == 0) {
			this.addActionError("** 客户名称不能为空 !");
		}
		this.setValidatorResult("/ndc/CustomerAdd.jsp");	
	}
	
	public String toEdit() {
//	    Long id = new Long(selectedId);
		String custId = selectedId;
	    
	    customer = customerService.getCustomerByCustId(custId);
	
		return RESULT_EDIT;
	}
	
	public String update() {
	
	    customerService.updateCustomer(customer);	

		message = "修改成功!";
	    
		return RESULT_EDIT;
	}
	
	public void validateUpdate() {
		if (customer.getCustId().length() == 0) {
			this.addActionError("** 客户编号不能为空 !");
		}
		if (customer.getGroupId().length() == 0) {
			this.addActionError("** 品牌编号不能为空 !");
		}
		if (customer.getName1().length() == 0) {
			this.addActionError("** 客户名称不能为空 !");
		}
		this.setValidatorResult("/ndc/CustomerUpdate.jsp");
	}
	
	public String delete() {
		for(String custId : selectedIds){
			customer = customerService.getCustomerByCustId(custId);
			
			try {
				customer.setCustStatus(new Long("1"));
				customerService.updateCustomer(customer);
			} catch (Exception e) {
				System.out.println("异常....");
				this.addActionError("** 可能含有子记录约束冲突删除失败 !");
			}
		}
		

		
		
		return queryByPage();
	}	
	
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	 public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	
	public Customer getCustomer() {
		return customer;
	}
	 
	 
	 public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	
	public List<Customer> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<Customer> customerList) {
		this.customerList = customerList;
	}
	
	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}
	
	    public String getQuery_custId() {
        return this.query_custId;
    }
    
    public void setQuery_custId(String custId){
        this.query_custId = custId;
    }    

        public String getQuery_groupId() {
        return this.query_groupId;
    }
    
    public void setQuery_groupId(String groupId){
        this.query_groupId = groupId;
    }    

        public String getQuery_name1() {
        return this.query_name1;
    }
    
    public void setQuery_name1(String name1){
        this.query_name1 = name1;
    }    

        public String getQuery_name2() {
        return this.query_name2;
    }
    
    public void setQuery_name2(String name2){
        this.query_name2 = name2;
    }    

        public String getQuery_addrId() {
        return this.query_addrId;
    }
    
    public void setQuery_addrId(String addrId){
        this.query_addrId = addrId;
    }    

        public String getQuery_mainListingId() {
        return this.query_mainListingId;
    }
    
    public void setQuery_mainListingId(String mainListingId){
        this.query_mainListingId = mainListingId;
    }    

        public String getQuery_mainListingStatus() {
        return this.query_mainListingStatus;
    }
    
    public void setQuery_mainListingStatus(String mainListingStatus){
        this.query_mainListingStatus = mainListingStatus;
    }    

        public String getQuery_grade() {
        return this.query_grade;
    }
    
    public void setQuery_grade(String grade){
        this.query_grade = grade;
    }    

        public String getQuery_nonpublic() {
        return this.query_nonpublic;
    }
    
    public void setQuery_nonpublic(String nonpublic){
        this.query_nonpublic = nonpublic;
    }    

        public String getQuery_custStatus() {
        return this.query_custStatus;
    }
    
    public void setQuery_custStatus(String custStatus){
        this.query_custStatus = custStatus;
    }    

        public String getQuery_note() {
        return this.query_note;
    }
    
    public void setQuery_note(String note){
        this.query_note = note;
    }    

        public String getQuery_modifyTime() {
        return this.query_modifyTime;
    }
    
    public void setQuery_modifyTime(String modifyTime){
        this.query_modifyTime = modifyTime;
    }    

        public String getQuery_modifyEmpId() {
        return this.query_modifyEmpId;
    }
    
    public void setQuery_modifyEmpId(String modifyEmpId){
        this.query_modifyEmpId = modifyEmpId;
    }    

        public String getQuery_custTypeCode() {
        return this.query_custTypeCode;
    }
    
    public void setQuery_custTypeCode(String custTypeCode){
        this.query_custTypeCode = custTypeCode;
    }    

        public String getQuery_classMainId() {
        return this.query_classMainId;
    }
    
    public void setQuery_classMainId(String classMainId){
        this.query_classMainId = classMainId;
    }    

        public String getQuery_orgId() {
        return this.query_orgId;
    }
    
    public void setQuery_orgId(String orgId){
        this.query_orgId = orgId;
    }    

        public String getQuery_url() {
        return this.query_url;
    }
    
    public void setQuery_url(String url){
        this.query_url = url;
    }    

        public String getQuery_weibo() {
        return this.query_weibo;
    }
    
    public void setQuery_weibo(String weibo){
        this.query_weibo = weibo;
    }    

        public String getQuery_weixin() {
        return this.query_weixin;
    }
    
    public void setQuery_weixin(String weixin){
        this.query_weixin = weixin;
    }    

        public String getQuery_yixin() {
        return this.query_yixin;
    }
    
    public void setQuery_yixin(String yixin){
        this.query_yixin = yixin;
    }    

        public String getQuery_logo() {
        return this.query_logo;
    }
    
    public void setQuery_logo(String logo){
        this.query_logo = logo;
    }    

        public String getQuery_safeStatus() {
        return this.query_safeStatus;
    }
    
    public void setQuery_safeStatus(String safeStatus){
        this.query_safeStatus = safeStatus;
    }    

        public String getQuery_bxName() {
        return this.query_bxName;
    }
    
    public void setQuery_bxName(String bxName){
        this.query_bxName = bxName;
    }    

        public String getQuery_pyName() {
        return this.query_pyName;
    }
    
    public void setQuery_pyName(String pyName){
        this.query_pyName = pyName;
    }    

        public String getQuery_engName() {
        return this.query_engName;
    }
    
    public void setQuery_engName(String engName){
        this.query_engName = engName;
    }

	public String getQuery_addr() {
		return query_addr;
	}

	public void setQuery_addr(String queryAddr) {
		query_addr = queryAddr;
	}

	public String getQuery_tel() {
		return query_tel;
	}

	public void setQuery_tel(String queryTel) {
		query_tel = queryTel;
	}    

    
	
}
